﻿using MaiPlanServer.Dto;
using MaiPlanServer.IRepository;
using MaiPlanServer.IServices.Production;
using MaiPlanServer.Model;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace MaiPlanServer.Services.Production
{
    public class ProductionPlanServices:IProductionPlanServices
    {
        public ProductionPlanServices(IBaseRepository conn)
        {
            Conn = conn;
        }

        public IBaseRepository Conn { get; }
        
        // 获取生产计划数据
        public async Task<IEnumerable<pro_workorder>> GetPro_Workorders(int PageIndex, int PageSize, string workorder_code, string workorder_name, string source_code, string product_code, string product_name, string client_code, string client_name, string request_date)
        {
                string sql = "select * from pro_workorder where workorder_code like @workorder_code and workorder_name like @workorder_name and source_code like @source_code and product_code like @product_code and product_name like @product_name and client_code like @client_code and client_name like @client_name and request_date like @request_date";
            sql += " order by create_time desc offset @PageSize*(@PageIndex-1) rows fetch next @PageSize row only";

            var pro = await Conn.QueryAsync<pro_workorder>(sql, new { PageIndex, PageSize, workorder_code = "%" + workorder_code + "%", workorder_name = "%" + workorder_name + "%", source_code = "%" + source_code + "%", product_code = "%" + product_code + "%", product_name = "%" + product_name + "%", client_code = "%" + client_code + "%", client_name = "%" + client_name + "%", request_date = "%" + request_date + "%" });



                return pro;
        }
        //获取生产计划条数
        public async Task<int> GetPro_WorkordersCount(string workorder_code, string workorder_name, string source_code, string product_code, string product_name, string client_code, string client_name, string request_date)
        {
            string sql = "select count(1) from pro_workorder where workorder_code like @workorder_code and workorder_name like @workorder_name and source_code like @source_code and product_code like @product_code and product_name like @product_name and client_code like @client_code and client_name like @client_name and request_date like @request_date";

            var count = await Conn.GetRecoreCountAsync(sql,new { workorder_code = "%" + workorder_code + "%", workorder_name = "%" + workorder_name + "%", source_code = "%" + source_code + "%", product_code = "%" + product_code + "%", product_name = "%" + product_name + "%", client_code = "%" + client_code + "%", client_name = "%" + client_name + "%", request_date = "%" + request_date + "%" });

            return count;
        }

        public async Task<IEnumerable<pro_workorderDto>> GetPro_WorkordersDto(string workorder_code)
        {
            var sql = "select * from pro_workorder pw inner join MD_ITEM mi on mi.ITEM_CODE=pw.product_code where workorder_code=@workorder_code";

            var list = await Conn.QueryAsync<pro_workorderDto>(sql,new { workorder_code });

            return list;
        }
    }
}
